/// -----------------------------------------------------------------------------------------------------------
/// Module      :  SalesForceWCFAdapterBinding.cs
/// Description :  This is the class used while creating a binding for an adapter
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace WCFLOBTalk.SalesForceWCF
{
    public class SalesForceWCFAdapterBinding : AdapterBinding
    {
        // Scheme in Binding does not have to be the same as Adapter Scheme. 
        // Over write this value as appropriate.
        private const string BindingScheme = "SalesForceWCF";



        /// <summary>
        /// Initializes a new instance of the AdapterBinding class
        /// </summary>
        public SalesForceWCFAdapterBinding() { 
        
            // set defaults - already done below

        
        }

        /// <summary>
        /// Initializes a new instance of the AdapterBinding class with a configuration name
        /// </summary>
        public SalesForceWCFAdapterBinding(string configName)
        {
            ApplyConfiguration(configName);
        }

        /// <summary>
        /// Applies the current configuration to the SalesForceWCFAdapterBindingCollectionElement
        /// </summary>
        private void ApplyConfiguration(string configurationName)
        {
            /*
            //
            // TODO : replace the <The config name of your adapter> below with the configuration name of your adapter
            //
            BindingsSection bindingsSection = (BindingsSection)System.Configuration.ConfigurationManager.GetSection("system.serviceModel/bindings");
            SalesForceWCFAdapterBindingCollectionElement bindingCollectionElement = (SalesForceWCFAdapterBindingCollectionElement)bindingsSection["<The config name of your adapter>"];
            SalesForceWCFAdapterBindingElement element = bindingCollectionElement.Bindings[configurationName];
            if (element != null)
            {
                element.ApplyConfiguration(this);
            }
            */
            throw new NotImplementedException("The method or operation is not implemented.");
        }



        #region Private Fields

        private SalesForceWCFAdapter binding;

        #endregion Private Fields

        #region Custom Generated Fields

        private string _Host = "";
        private HostTypeEnum _HostType = HostTypeEnum.u;
        private string _Version = "";
        private ProtocolTypesEnum _Protocol = ProtocolTypesEnum.SOAP;
        private string _SecurityToken = "";
        private string _Username = "";
        private string _Password = "";

        #endregion Custom Generated Fields

        #region Public Properties

        /// <summary>
        /// Gets the URI transport scheme that is used by the channel and listener factories that are built by the bindings.
        /// </summary>
        public override string Scheme
        {
            get
            {
                return BindingScheme;
            }
        }

        /// <summary>
        /// Returns a value indicating whether this binding supports metadata browsing.
        /// </summary>
        public override bool SupportsMetadataBrowse
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Returns a value indicating whether this binding supports metadata retrieval.
        /// </summary>
        public override bool SupportsMetadataGet
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Returns a value indicating whether this binding supports metadata searching.
        /// </summary>
        public override bool SupportsMetadataSearch
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Returns the custom type of the ConnectionUri.
        /// </summary>
        public override Type ConnectionUriType
        {
            get
            {
                return typeof(SalesForceWCFAdapterConnectionUri);
            }
        }

        #endregion Public Properties

        #region Custom Generated Properties

        [ConfigurationProperty("host", DefaultValue="test.salesforce.com")]
        public string Host
        {
            get
            {
                return this._Host;
            }
            set
            {
                this._Host = value;
            }
        }

        [ConfigurationProperty("hostType", DefaultValue=HostTypeEnum.u)]
        public HostTypeEnum HostType
        {
            get
            {
                return this._HostType;
            }
            set
            {
                this._HostType = value;
            }
        }

        [ConfigurationProperty("version", DefaultValue="8.0")]
        public string Version
        {
            get
            {
                return this._Version;
            }
            set
            {
                this._Version = value;
            }
        }

        [ConfigurationProperty("protocol", DefaultValue=ProtocolTypesEnum.SOAP)]
        public ProtocolTypesEnum Protocol
        {
            get
            {
                return this._Protocol;
            }
            set
            {
                this._Protocol = value;
            }
        }

        [ConfigurationProperty("securityToken", DefaultValue="")]
        public string SecurityToken
        {
            get
            {
                return this._SecurityToken;
            }
            set
            {
                this._SecurityToken = value;
            }
        }

        [ConfigurationProperty("username", DefaultValue = "")]
        public string Username
        {
            get
            {
                return this._Username;
            }
            set
            {
                this._Username = value;
            }
        }

        [ConfigurationProperty("password", DefaultValue = "")]
        public string Password
        {
            get
            {
                return this._Password;
            }
            set
            {
                this._Password = value;
            }
        }


        #endregion Custom Generated Properties

        #region Private Properties

        private SalesForceWCFAdapter BindingElement
        {
            get
            {
                if (binding == null)
                    binding = new SalesForceWCFAdapter();

                binding.Host = this.Host;
                binding.HostType = this.HostType;
                binding.Protocol = this.Protocol;
                binding.SecurityToken = this.SecurityToken;
                binding.Version = this.Version;
                binding.Username = this.Username;
                binding.Password = this.Password;

                return binding;
            }
        }

        #endregion Private Properties

        #region Public Methods

        /// <summary>
        /// Creates a clone of the existing BindingElement and returns it
        /// </summary>
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElementCollection bindingElements = new BindingElementCollection();
            //Only create once
            bindingElements.Add(this.BindingElement);
            //Return the clone
            return bindingElements.Clone();
        }

        #endregion Public Methods

    }
}
